package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.chuanye.jiguangimdemo.R;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

public class NotFriendSettingActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "NotFriendSettingActivity";
    private Context mContext;

    private ImageView notfriendsetting_back_iv;
    private RelativeLayout notfriendsetting_businesscard_rl;
    private ToggleButton notfriendsetting_addblacklist_tbtn;


    private UserInfo mUserInfo;
    private String mUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_not_friend_setting);
        mContext = this;
        mUserName = getIntent().getStringExtra("notFriendUserName");

        initView();
        JMessageClient.getUserInfo(mUserName, new GetUserInfoCallback() {
            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                if (i == 0) {
                    mUserInfo = userInfo;
                    notfriendsetting_addblacklist_tbtn.setChecked(userInfo.getBlacklist() == 1);
                }
            }
        });
    }

    private void initView() {
        //返回
        notfriendsetting_back_iv = findViewById(R.id.notfriendsetting_back_iv);
        notfriendsetting_back_iv.setOnClickListener(this);
        //发送名片
        notfriendsetting_businesscard_rl = findViewById(R.id.notfriendsetting_businesscard_rl);
        notfriendsetting_businesscard_rl.setOnClickListener(this);
        //加入黑名单
        notfriendsetting_addblacklist_tbtn = findViewById(R.id.notfriendsetting_addblacklist_tbtn);
        //加入黑名单
        notfriendsetting_addblacklist_tbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                List<String> name = new ArrayList<>();
                name.add(mUserName);
                if (isChecked) {
                    JMessageClient.addUsersToBlacklist(name, new BasicCallback() {
                        @Override
                        public void gotResult(int responseCode, String responseMessage) {
                            if (responseCode == 0) {
                                Toast.makeText(NotFriendSettingActivity.this, "添加成功",Toast.LENGTH_SHORT).show();
                            } else {
                                notfriendsetting_addblacklist_tbtn.setChecked(false);
                                Toast.makeText(NotFriendSettingActivity.this, "添加失败" + responseMessage,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } else {
                    JMessageClient.delUsersFromBlacklist(name, new BasicCallback() {
                        @Override
                        public void gotResult(int responseCode, String responseMessage) {

                            if (responseCode == 0) {
                                Toast.makeText(NotFriendSettingActivity.this, "移除成功",Toast.LENGTH_SHORT).show();
                            } else {
                                notfriendsetting_addblacklist_tbtn.setChecked(true);
                                Toast.makeText(NotFriendSettingActivity.this, "移除失败" + responseMessage,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.notfriendsetting_back_iv://返回
                finish();
                break;
            case R.id.notfriendsetting_businesscard_rl://发送此人名片
                //发送此人的名片
                Intent businessIntent = new Intent(NotFriendSettingActivity.this, ForwardMsgActivity.class);
                businessIntent.setFlags(1);
                businessIntent.putExtra("userName", mUserInfo.getUserName());
                businessIntent.putExtra("appKey", mUserInfo.getAppKey());
                if (mUserInfo.getAvatarFile() != null) {
                    businessIntent.putExtra("avatar", mUserInfo.getAvatarFile().getAbsolutePath());
                }
                startActivity(businessIntent);
                break;
        }
    }
}
